package com.pro.service.imple;

import com.pro.mapper.JsAuthorityMapper;
import com.pro.pojo.JsUser;
import com.pro.pojo.LoginUser;
import com.pro.service.JsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * ClassName:SecurityService
 * Package:com.pro.security
 * Description:
 *
 * @Date:2021/11/6 17:38
 * @Author: LBF
 */
@Component
public class SecurityServiceImple implements UserDetailsService {

    @Autowired
    private JsUserService jsUserService;

    @Autowired
    private JsAuthorityMapper jsAuthorityMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        JsUser user = jsUserService.loginByName(username);
//        System.out.println("用户"+user);
        if (user == null){
            throw new UsernameNotFoundException("用户名错误");
        }
        //三个参数为，用户名、用户密码、用户权限集合(没有权限，创建一个空的集合)
        List<String> list = jsAuthorityMapper.getPermsByUserId(user.getUserId());
       /* org.springframework.security.core.userdetails.User result = new org.springframework.security.core.userdetails.User(
                username,user.getPassword(),AuthorityUtils.createAuthorityList()
        );*/
//        System.out.println("认证");
        return new LoginUser(user, list);
    }



}
